package com.helloworld.demo;

public class DeadLockTest {

    public static void main(String[] args) {
        Object lock1 = new Object();
        Object lock2 = new Object();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock1){
                    System.out.println(Thread.currentThread().getName()+"获取到Lock1");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock2){
                        System.out.println(Thread.currentThread().getName()+"请求lock2");
                    }
                }
            }
        },"Thread1").start();

        new Thread(()->{
            synchronized (lock2){
                System.out.println(Thread.currentThread().getName()+"获取到Lock2");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock1){
                    System.out.println(Thread.currentThread().getName()+"请求lock1");
                }
            }
        },"Thread2").start();
        System.out.println("主线程结束");
    }
}
